package com.jiang.专题.字符串.Q38;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/02/27 18:08
 */
class Solution {
    public String countAndSay(int n) {
        if (n == 1) return "1";
        char[] arr = countAndSay(n - 1).toCharArray();
        StringBuilder sb = new StringBuilder();
        int cnt = 1;
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] == arr[i - 1]) {
                cnt++;
            } else {
                sb.append(cnt).append(arr[i - 1]);
                cnt = 1;
            }
        }
        sb.append(cnt).append(arr[arr.length - 1]);
        return sb.toString();
    }
}
